

// pages/edit/edit.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iconimg:'../../public/discovery-img/selectimg.png',
        item:{},
        imgList: [],
        error:''
    },
    getIndex(){ 
        var unionids=[];
        var that=this;
        const index=wx.getStorageSync('index')
        unionids=wx.getStorageSync('union_id')
        const union_id=unionids[index]
        wx.request({
            url: 'http://localhost:3000/getInfo',
            method:'GET',
            data:{
                union_id:union_id
            },
            success(res){
                if(res.data.code==200){
                that.setData({
                    item:res.data.data[0]
                })
                if(that.data.item.img_name==null){

                }else{
                that.setData({
                    imgList: [
                        {
                            tempFilePath: 'data:image/'+that.data.item.img_type+';base64,' + that.data.item.img_name,
                            name: that.data.item.img_name
                        }
                    ]
                });
            }
                }else{
                    console.log(res);
                }
            }
        })
    },
    deleteimg:function(){
        let img = this.data.imgList
        if(img){
            this.setData({
                imgList:[]
            })
        }
    },
    selectimg:function(){
        wx.chooseMedia({
            count:1,
            mediaType:['image','video'],
            sourceType:['album','camera'],
            maxDuration:30,
            camera:['back','front'],
            success:(res)=>{
                this.setData({
                    imgList:[res.tempFiles[0]]
                })
 
            }
        })
    },
    up(){
        let unionids = wx.getStorageSync('union_id')
        let index=wx.getStorageSync('index')
        let union_id=unionids[index]
       
        wx.uploadFile({
          filePath: this.data.imgList[0].tempFilePath,
          name: 'file',
          url: 'http://localhost:3000/updateImage',
          formData:{
            'union_id':union_id
          },
          success:(res)=>{
            console.log(res);
          }
        })
      },
        formupdate(e){
            var that=this
            let img = that.data.imgList
            let index=wx.getStorageSync('index')
            let unionds=wx.getStorageSync('union_id')
            let union_id=unionds[index]
        e.detail.value.union_id=union_id
        if(e.detail.value.title && e.detail.value.text){
            if(img.length==0){
                
                that.deleteImage()
                wx.request({
                  url: 'http://localhost:3000/formupdate',
                  method:'POST',
                  data:{
                      e:e.detail.value
                  },
                  success(res){
                  console.log(res)
                      
                      if(res.data.code == 200){
                          wx.hideLoading()
                          wx.showToast({
                            title: '成功',
                            icon:"success",
                            duration:2000,
                            success:()=>{
                              wx.navigateBack({
                                  delta:1
                              })
                            }
                            
                          })
                      }
                  }
                })
                
            }else{
                wx.request({
                    url: 'http://localhost:3000/formupdate',
                    method:'POST',
                    data:{
                        e:e.detail.value
                    },
                    success(res){
                        console.log(res)
                          if(res.data.code == 200){
                              that.up()
                              wx.hideLoading()
                              wx.showToast({
                                title: '成功',
                                icon:"success",
                                duration:2000,
                                success:()=>{
                                  wx.navigateBack({
                                      delta:1
                                  })
                                }
                              })
          
              
                          }
                    }
                
    
    
            })
        
        }
        }else{
            this.setData({
                error:'请填写内容'
            })
        }
        
    },
    deleteImage(){
        let index=wx.getStorageSync('index')
            let unionds=wx.getStorageSync('union_id')
            let union_id=unionds[index]
        wx.request({
            url: 'http://localhost:3000/deleteImage',
            method:'POST',
            data:{
                union_id:union_id
            },
            success(res){
                console.log(res)
            }
          })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
      this.getIndex()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})